﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="RenderTargetVectorLineRenderer.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.twoD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using starLiGHT.ParticleSystem.Common;
    using starLiGHT.Render;
    using starLiGHT.SceneManagement;

    #endregion

#if !XNA4 && !ZUNE && !SILVERLIGHT
    /// <summary>
    /// The RenderTargetRenderer draws particles onto a single RenderTarget. The region of the particle 
    /// system is covered by the RenderTargets dimensions. Particles outside this region are not drawn.
    /// <br />
    /// The texture to be used for each particle is the particle's image property.
    /// <br />
    /// </summary>
    public class RenderTargetVectorLineRenderer : SpriteRendererBase
    {
        #region Private Members
        private Rectangle _canvas;
        private bool _smoothing;
        private bool _clearBetweenFrames;
        private RenderTarget2D _renderTarget;
        private GraphicsDevice _graphics;
        private PrimitiveBatch _batch;
        #endregion

        #region Constructors
        public RenderTargetVectorLineRenderer(GraphicsDevice graphics) 
            : this(new Rectangle(0, 0, 800, 600), false, graphics) 
        { 
        }

        public RenderTargetVectorLineRenderer(Rectangle canvas, GraphicsDevice graphics) 
            : this(canvas, false, graphics) 
        { 
        }

        public RenderTargetVectorLineRenderer(Rectangle canvas, bool smoothing, GraphicsDevice graphics)
            : base()
        {
            this._graphics = graphics;
            this._smoothing = smoothing;
            this._canvas = canvas;
            createRenderTarget(this._graphics);
            createSpriteBatch(this._graphics);
            this._clearBetweenFrames = true;
        }
        #endregion

        ~RenderTargetVectorLineRenderer()
        {
            this._graphics = null;
            if (this._batch != null)
            {
                this._batch.Dispose();
                this._batch = null;
            }

            if (this._renderTarget != null)
            {
                this._renderTarget.Dispose();
                this._renderTarget = null;
            }
        }

        protected void createRenderTarget(GraphicsDevice graphics)
        {
            if (this._canvas.IsEmpty)
            {
                return;
            }

            if (this._renderTarget != null)
            {
                this._renderTarget.Dispose();
            }

#if !XNA4
            this._renderTarget = new RenderTarget2D(graphics, this._canvas.Width, this._canvas.Height, 1, SurfaceFormat.Color);
#else
            this._renderTarget = new RenderTarget2D(graphics, this._canvas.Width, this._canvas.Height, false, SurfaceFormat.Color, DepthFormat.None);
#endif
        }

        protected void createSpriteBatch(GraphicsDevice graphics)
        {
            this._batch = new PrimitiveBatch(graphics);
        }

        public bool ClearBetweenFrames
        {
            get { return this._clearBetweenFrames; }
            set { this._clearBetweenFrames = value; }
        }

        public Texture2D Texture
        {
#if !XNA4
            get { return this._renderTarget.GetTexture(); }
#else
            get { return this._renderTarget; }
#endif
        }

        public override string Type
        {
            get { return RenderTargetVectorLineRendererFactory.RENDER_TARGET_RENDERER_FACTORY; }
        }

        public override void renderParticles(Particle[] particles)
        {
            this._graphics.SetRenderTarget(0, _renderTarget);

            this._graphics.Clear(Color.TransparentBlack);

            this._batch.Begin(PrimitiveType.LineList);
            this._batch.ProjectionMatrix = Matrix.CreateOrthographic(this._renderTarget.Width, this._renderTarget.Height, 0, 1);
            this._batch.WorldMatrix = Matrix.Identity;
            this._batch.ViewMatrix = Matrix.CreateTranslation(this._renderTarget.Width * -0.5f, this._renderTarget.Height * -0.5f, 0);

#if !ZUNE
            this._graphics.RenderState.AlphaBlendEnable = true;
            this._graphics.RenderState.SourceBlend = Blend.One;
            this._graphics.RenderState.DestinationBlend = Blend.InverseSourceAlpha; 
#endif

            foreach (Emitter2D emitter in Emitters.Values)
            {
                foreach (Particle2D p in emitter.Particles)
                {
                    this._batch.AddVertex(p.position, p.color);
                    this._batch.AddVertex(p.previousPosition, p.color);
                }
            }

            this._batch.End();

            this._graphics.SetRenderTarget(0, null);
        }
    }

    public class RenderTargetVectorLineRendererFactory : IParticleSystemRendererFactory
    {
        public static string RENDER_TARGET_RENDERER_FACTORY = "RenderTargetVectorLineRenderer";

        public string Type
        {
            get { return RENDER_TARGET_RENDERER_FACTORY; }
        }

        public starLiGHT.ParticleSystem.Common.IRenderer createInstance(string name, GraphicsDevice graphics)
        {
            return new RenderTargetVectorLineRenderer(graphics);
        }

        public void destroyInstance(starLiGHT.ParticleSystem.Common.IRenderer renderer)
        {
            ////////TODO: renderer.Dispose();
        }
    }
#endif
}
